﻿using System.Web.Mvc;

namespace EPiBoost.Core.Infrastructure
{
	public class AreaTemplateViewEngine : RazorViewEngine
    {
        public AreaTemplateViewEngine() : base()
        {
			//AreaViewLocationFormats = new[]
			//                              { 
			//                                  "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
			//                                  "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
			//                                  "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
			//                                  "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
			//                              };

			//AreaMasterLocationFormats = new[]
			//                                { 
			//                                    "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
			//                                    "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
			//                                    "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
			//                                    "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
			//                                };

			//AreaPartialViewLocationFormats = new[]
			//                                     { 
			//                                         "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
			//                                         "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
			//                                         "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
			//                                         "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
			//                                     };

            ViewLocationFormats = new[]
                                      { 
										  //"~/Views/%1/{1}/{0}.cshtml",
										  //"~/Views/%1/{1}/{0}.vbhtml",
										  //"~/Views/%1/Shared/{0}.cshtml",
										  //"~/Views/%1/Shared/{0}.vbhtml",
                                          "~/Areas/%1/Views/{1}/{0}.cshtml",
                                          "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                          "~/Areas/%1/Views/Shared/{0}.cshtml",
                                          "~/Areas/%1/Views/Shared/{0}.vbhtml",
                                          "~/Areas/%1/Views/{1}/{0}.cshtml",
                                          "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                          "~/Areas/%1/Views/Shared/{0}.cshtml",
                                          "~/Areas/%1/Views/Shared/{0}.vbhtml"
                                      };

            MasterLocationFormats = new[]
                                        { 
											//"~/Views/%1/{1}/{0}.cshtml",
											//"~/Views/%1/{1}/{0}.vbhtml",
											//"~/Views/%1/Shared/{0}.cshtml",
											//"~/Views/%1/Shared/{0}.vbhtml",
                                            "~/Areas/%1/Views/{1}/{0}.cshtml",
                                            "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                            "~/Areas/%1/Views/Shared/{0}.cshtml",
                                            "~/Areas/%1/Views/Shared/{0}.vbhtml",
                                            "~/Areas/%1/Views/{1}/{0}.cshtml",
                                            "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                            "~/Areas/%1/Views/Shared/{0}.cshtml",
                                            "~/Areas/%1/Views/Shared/{0}.vbhtml"
                                        };

            PartialViewLocationFormats = new[]
                                             { 
												 //"~/Views/%1/{1}/{0}.cshtml",
												 //"~/Views/%1/{1}/{0}.vbhtml",
												 //"~/Views/%1/Shared/{0}.cshtml",
												 //"~/Views/%1/Shared/{0}.vbhtml",
												 "~/Views/Shared/BlockTemplates/{0}.cshtml",
												 "~/Views/Shared/BlockTemplates/{0}.vbhtml",
                                                 "~/Areas/%1/Views/{1}/{0}.cshtml",
                                                 "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                                 "~/Areas/%1/Views/Shared/{0}.cshtml",
                                                 "~/Areas/%1/Views/Shared/{0}.vbhtml",
                                                 "~/Areas/%1/Views/{1}/{0}.cshtml",
                                                 "~/Areas/%1/Views/{1}/{0}.vbhtml",
                                                 "~/Areas/%1/Views/Shared/{0}.cshtml",
                                                 "~/Areas/%1/Views/Shared/{0}.vbhtml"
                                             };
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var areaName = GetAreaFromNameSpace(controllerContext);
            return base.CreatePartialView(controllerContext, partialPath.Replace("%1", areaName));
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var areaName = GetAreaFromNameSpace(controllerContext);
            return base.CreateView(controllerContext, viewPath.Replace("%1", areaName), masterPath.Replace("%1", areaName));
        }

        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            var areaName = GetAreaFromNameSpace(controllerContext);
            if (!string.IsNullOrEmpty(areaName))
            {
                var exists = base.FileExists(controllerContext, virtualPath.Replace("%1", areaName));
                if (exists)
                {
                    return true; // for debugging
                }
            }
            return false;
        }

        private string GetAreaFromNameSpace(ControllerContext controllerContext)
        {
            if (controllerContext.Controller != null)
            {
                var nameSpace = controllerContext.Controller.GetType().Namespace;
                var namespaceSegments = nameSpace.Split('.');
                if (namespaceSegments.Length >= 2)
                {
                    return namespaceSegments[namespaceSegments.Length - 2];
                }
            }
            return null;
        }
    }
}